﻿using ServerBase.Config;
using ServerBase.Protocol;
using System;
using System.Collections.Generic;
using System.Text;
using UtilPublic.Serialization;

namespace ServerBase.LunarSocket
{
    /// <summary>
    /// 协议请求
    /// </summary>
    public class LunarRequestInfo
    {
        public string _body = "";
        public short _protocol = -1;
        public short _result = -1;

        public LunarRequestInfo()
        {
        }
        public LunarRequestInfo(string body)
        {
            _body = body;
            var msgbase = Jsh.Jobj<ProtocolMsgBase>(Body);
            _protocol = msgbase._protocol;
            _result = msgbase._result;
        }
        public static LunarRequestInfo From<T>(T t)
        {
            return new LunarRequestInfo(Jsh.Jstr(t));
        }
        public T To<T>()
        {
            return Jsh.Jobj<T>(Body);
        }

        public string Body
        {
            get
            {
                return _body;
            }
        }
        public int Length
        {
            get
            {
                return _body.Length;
            }
        }
        public EProtocolId ProtocolID
        {
            get
            {
                if (_protocol == -1)
                {
                    var msgbase = Jsh.Jobj<ProtocolMsgBase>(Body);
                    _protocol = msgbase._protocol;
                    _result = msgbase._result;
                }
                return (EProtocolId)_protocol;
            }
        }
        public EProtocolResult Result
        {
            get
            {
                if (_result == -1)
                {
                    var msgbase = Jsh.Jobj<ProtocolMsgBase>(Body);
                    _protocol = msgbase._protocol;
                    _result = msgbase._result;
                }
                return (EProtocolResult)_result;
            }
        }
    }
}
